﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Devices.Sensors;

namespace Spinning.Spinning.Entity
{
    class AccScene : BaseScene
    {
        Vector3 _accelReading = new Vector3();
        AnimationSprite animationSprite;
        Vector2 _acc = new Vector2();

        public AccScene() : base()
        {
            Size = new Vector2(1);
            Game1._acceleMeter.CurrentValueChanged += GSensorChanged;
            try
            {
                Game1._acceleMeter.Start();
            }
            catch (AccelerometerFailedException e) {
                throw e;
            }
            catch (UnauthorizedAccessException e) {
                throw e;
            }
        }

        void GSensorChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            _accelReading.X = (float)e.SensorReading.Acceleration.X;
            _accelReading.Y = -(float)e.SensorReading.Acceleration.Y;
            _accelReading.Z = (float)e.SensorReading.Acceleration.Z;
        }

        public override void OnLoad()
        {
 	        base.OnLoad();
            animationSprite = new AnimationSprite();
            animationSprite.textureName = "spinning";
            animationSprite.Origin = new Vector2(1f);
            animationSprite.Position = new Vector2(130, 150);
            animationSprite.OnLoad();
            animationSprite.SetAnimation(Constants.playerAnimationFrame);
            animationSprite.DeltaTime = 80;
            this.Attach(animationSprite);
            
            
            TimeCountBar t1 = new TimeCountBar(new Vector2(35,30),10 ,"water");
            this.Attach(t1);
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            _acc.X += _accelReading.X;
            _acc.Y += _accelReading.Y;
            int terminalVelocity = 6;
            if (_acc.X > terminalVelocity) _acc.X = terminalVelocity;
            if (_acc.X < -terminalVelocity) _acc.X = -terminalVelocity;

            if (_acc.Y > terminalVelocity) _acc.Y = terminalVelocity;
            if (_acc.Y < -terminalVelocity) _acc.Y = -terminalVelocity;

            if(_acc.Y>5)animationSprite.SetChangeFrameAnimation(new int[] {4,5});
            else if (_acc.Y > -5) animationSprite.SetChangeFrameAnimation(new int[] { 2, 3 });
            else animationSprite.SetChangeFrameAnimation(new int[] {0,1});

            Vector2 pos = new Vector2();
            pos.X = animationSprite.Position.X + (_acc.X) * 1.5f;
            pos.Y = animationSprite.Position.Y + (_acc.Y) * 1.5f;
            if (pos.X < 0 + animationSprite._frameSize.X / 2) pos.X = animationSprite.Position.X;
            if (pos.X > 480 - animationSprite._frameSize.X / 2) pos.X = animationSprite.Position.X;
            if (pos.Y < 0 + animationSprite._frameSize.Y / 2) pos.Y = animationSprite.Position.Y;
            if (pos.Y > 800 - animationSprite._frameSize.Y / 2) pos.Y = animationSprite.Position.Y;
            animationSprite.Position = new Vector2(pos.X, pos.Y);
        }

        public override BaseScene Clone()
        {
            BaseScene scene = new AccScene();
            scene.Size = new Vector2(480, 800);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
